package com.spoto.oa.servlet;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;

import com.spoto.oa.dao.SalesDao;
import com.spoto.oa.dao.impl.ProductDaoImpl;
import com.spoto.oa.dao.impl.SalesDaoImpl;
import com.spoto.oa.model.Employee;
import com.spoto.oa.model.Sales;
import com.spoto.oa.util.ChartUtil;
import com.spoto.oa.util.DateUtil;

public class ChartServlet extends HttpServlet {

	public ChartServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 显示样式（是否为3D效果）
		Date dateBegin = DateUtil
				.parseToDate(request.getParameter("dateBegin"));
		Date dateEnd = DateUtil.parseToDate(request.getParameter("dateEnd"));
		String type = request.getParameter("searchType");
		String style = request.getParameter("style");
		String fileName = null; // 生成图片的文件名
		HttpSession session = request.getSession();
		Employee loginEmployee = (Employee) session.getAttribute("login_emp");
		// 判断是否管理员
		boolean role = loginEmployee.equals("是") ? true : false;
		fileName = ServletUtilities.saveChartAsJPEG(ChartUtil.createChart(
				style, dateBegin, dateEnd, type, loginEmployee.getUsername(),
				role), 1000, 300, request.getSession());

		// 获取图片的路径
		String graphURL = request.getContextPath() + "/DisplayChart?filename="
				+ fileName;
		// 将路径放到request对象中
		request.setAttribute("graphURL", graphURL);
		// 传递传进Servlet的参数以便供应输入框记住
		request.setAttribute("styleType", style);
		request.setAttribute("searchTypeValue", type);
		if (dateBegin != null && dateEnd != null) {
			request.setAttribute("dateBeginValue", dateBegin.toString());
			request.setAttribute("dateEndValue", dateEnd.toString());
		}
		// 页面转发到result.jsp
		request.getRequestDispatcher("sales/welcome_chart.jsp").forward(
				request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init() throws ServletException {
	}

}
